﻿using System;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace lapinou_game2
{
    class ListBox
    {
        Sprite top_link, top_over;
        Sprite[] itemBox;
        ContentManager Content;
        Vector2 position;
        List<string> itemList;
        SpriteFont font;

        public Sprite[] ItemBox
        {
            get
            {
                return itemBox;
            }
            set
            {
                itemBox = value;
            }
        }

        // Initialize
        public ListBox(ContentManager Content, List<string> item, Vector2 position)
        {
            this.Content = Content;
            this.itemList = item;
            this.position = position;

            int tempPosY = (int)position.Y + 35;

            top_link = new Sprite(position);
            top_over = new Sprite(position);

            itemBox = new Sprite[itemList.Count];

            for (int i = 0; i < itemList.Count; i++)
            {
                itemBox[i] = new Sprite(position.X, tempPosY);
                tempPosY += 25;
            }
        }

        public string getValue(Cursor cursor)
        {
            if (cursor.buttonPressed() == "left")
            {
                int xtemp = 0, ytemp = 0;
                for (int i = 0; i < itemList.Count; i++)
                {
                    xtemp = (int)itemBox[i].Position.X;
                    ytemp = (int)itemBox[i].Position.Y;
                    if (cursor.isRectangleOver(xtemp, ytemp, xtemp + 200, ytemp + 20))
                        return itemList[i];
                }
            }
            return "";
        }

        public void loadListBox()
        {
            // chargement de la police d'ecriture
            font = Content.Load<SpriteFont>("other/listboxtext");

            // load pictures
            top_link.LoadContent(Content, "image/components/combobox_top_link");
            top_over.LoadContent(Content, "image/components/combobox_top_over");

            // on charge chaque fond de case de listbox
            for (int i = 0; i < itemList.Count; i++)
            {
                itemBox[i].LoadContent(Content, "image/components/combobox_item");
            }
        }

        int isMenuScroll = -1;

        public void drawListBox(SpriteBatch spriteBatch, Cursor cursor)
        {
            bool overListBoxButton = cursor.isRectangleOver((int)position.X, (int)position.Y, (int)position.X + 200, (int)position.Y + 35);
            bool leftButtonPressed = cursor.buttonPressed() == "left";
            
            if (isMenuScroll == -1)
                isMenuScroll = 0;

            if (leftButtonPressed && overListBoxButton && isMenuScroll == 0)
            {
                isMenuScroll = 1;
                generateListBox(spriteBatch);
            }
            else if (leftButtonPressed && overListBoxButton && isMenuScroll == 1)
            {
                top_link.Draw(spriteBatch);
            }
            else if (isMenuScroll == 1)
            {
                if (leftButtonPressed)
                {
                    isMenuScroll = 0;
                    top_link.Draw(spriteBatch);
                }
                else
                    generateListBox(spriteBatch);
            }
            else
            {
                isMenuScroll = 0;
                top_link.Draw(spriteBatch);
            }

        }

        private void generateListBox(SpriteBatch spriteBatch)
        {
            top_over.Draw(spriteBatch);
            // on génère les cases d'option
            int i = 0;
            int tempPosY = (int)position.Y + 35;

            foreach (String item in itemList)
            {
                itemBox[i].Draw(spriteBatch);
                spriteBatch.DrawString(font, item.ToString(), new Vector2(position.X + 5, tempPosY), Color.Black);
                tempPosY += 25;
                i++;
            }
        }
    }
}
